package com.cskaoyan.order.controller;

import com.alibaba.fastjson.JSON;
import com.cskaoyan.bean.Userinfo;
import com.cskaoyan.mall.commons.result.ResponseData;
import com.cskaoyan.mall.commons.result.ResponseUtil;
import com.cskaoyan.order.dto.OrderDetailResponse;
import com.cskaoyan.order.dto.OrderListResponse;
import com.cskaoyan.order.form.OrderCancelRequest;
import com.cskaoyan.order.form.CreateOrderRequest;
import com.cskaoyan.order.form.OrderListRequest;
import com.cskaoyan.order.service.OrderCoreService;
import com.cskaoyan.order.service.OrderQueryService;
import com.cskaoyan.order.utils.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.UUID;

/**
 * @ClassName OrderController
 * @Description TODO
 * @Author zhangdongdong
 * @Date 2022/9/20 17:20
 * @Version 1.0
 */
@RestController
@RequestMapping("/shopping")
public class OrderController {

    @Autowired
    OrderCoreService orderCoreService;
    @Autowired
    OrderQueryService orderQueryService;

    /** 
     * @description: 提交订单
     * @param: orderRequest
servletRequest
     * @return: com.cskaoyan.mall.commons.result.ResponseData
     * @author zhangdongdong
     * @date: 2022/9/21 14:14
     */ 
    @PostMapping("/order")
    public ResponseData giveOrder(@RequestBody CreateOrderRequest orderRequest, HttpServletRequest servletRequest) {
        //从servletRequest中拿到用户信息
        Userinfo userInfo = UserUtil.getUserInfo(servletRequest);

        //userId
        orderRequest.setUserId(userInfo.getUid());

        //uniqueId
        orderRequest.setUniqueKey(UUID.randomUUID().toString());

        //调用service
        String orderId = orderCoreService.createOrder(orderRequest);

        return new ResponseUtil<>().setData(orderId);
    }


    @GetMapping("order")
    public ResponseData order(OrderListRequest orderListRequest, HttpServletRequest request) {
        String user_info = request.getHeader("user_info");
        Userinfo userinfo = JSON.parseObject(user_info, Userinfo.class);
        orderListRequest.setUserId(userinfo.getUid());
        OrderListResponse response = orderQueryService.orderList(orderListRequest);
        return new ResponseUtil<OrderListResponse>().setData(response);
    }


    /**
    * @description: 查看订单的详情
    []
    * @return: com.cskaoyan.mall.commons.result.ResponseData
    * @author: 杨庆
    * @time: 2022.9.21 9:55
     */
    @RequestMapping("order/{id}")
    public ResponseData selectOrderInfo(@PathVariable("id") String id) {
        // 接收的是订单的id  返回的结果OrderDetailResponse
        OrderDetailResponse orderDetailResponse = orderQueryService.orderDetail(id);
        return new ResponseUtil<>().setData(orderDetailResponse);
    }


    @DeleteMapping("/order/{id}")
    public ResponseData deleteOrder (@PathVariable Long id){
        String str = String.valueOf(id);
        orderCoreService.deleteOrder(str);
        return new ResponseUtil<>().setData("成功");
    }

    @RequestMapping("cancelOrder")
    public ResponseData cancelOrder(@RequestBody OrderCancelRequest orderCancelRequest){
        orderCoreService.cancelOrder(orderCancelRequest.getOrderId());
        return new ResponseUtil<>().setData("成功");
    }

}



